package com.youlai.boot.form.controller;

import com.youlai.boot.bmp.model.vo.ProcessCategoryVO;
import com.youlai.boot.core.web.PageResult;
import com.youlai.boot.core.web.Result;
import com.youlai.boot.form.service.FormDefinitionService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.form.model.form.FormDefinitionForm;
import com.youlai.boot.form.model.query.FormDefinitionQuery;
import com.youlai.boot.form.model.vo.FormDefinitionVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 动态单定义前端控制层
 *
 * @author rabit
 * @since 2025-10-27 16:47
 */
@Tag(name = "22.动态单定义接口")
@RestController
@RequestMapping("/api/v1/form")
@RequiredArgsConstructor
public class FormDefinitionController {

    private final FormDefinitionService formDefinitionService;


    @Operation(summary = "动态单定义分页列表")
    @PostMapping("/page")
    @PreAuthorize("@ss.hasPerm('form:definition:query')")
    public PageResult<FormDefinitionVO> getFormDefinitionPage(@RequestBody FormDefinitionQuery queryParams) {
        if (queryParams.getHistory().equals(1) && StringUtils.isEmpty(queryParams.getFormCode())){
            throw new IllegalArgumentException("参数错误:查询历史表单时表单编码必传");
        }
        IPage<FormDefinitionVO> result = formDefinitionService.getFormDefinitionPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增或保存动态单定义")
    @PostMapping("save")
    @PreAuthorize("@ss.hasPerm('form:definition:add')")
    public Result<Void> saveFormDefinition(@RequestBody @Valid FormDefinitionForm formData) {
        Long result = formDefinitionService.saveOrUpdateFormDefinition(formData);
        return Result.judge(result > 0);
    }

    @Operation(summary = "获取动态单定义表单数据")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('form:definition:query')")
    public Result<FormDefinitionVO> getFormDefinitionForm(@Parameter(description = "动态单定义ID") @PathVariable Long id) {
        FormDefinitionVO data = formDefinitionService.getFormDefinitionFormData(id);
        return Result.success(data);
    }

    @Operation(summary = "表单状态切换")
    @GetMapping("/switch")
    @PreAuthorize("@ss.hasPerm('form:definition:edit')")
    public Result<Void> switchFormDefinitionFormStatus(@Parameter(description = "动态单定义ID") @RequestParam Long id, @Parameter(description = "动态单定义状态") @RequestParam Integer status) {
        if (status != 1 && status != 0) {
            return Result.failed("参数错误");
        }
        boolean result = formDefinitionService.updateFormDefinitionStatus(id, status);
        return Result.judge(result);
    }

    @Operation(summary = "删除动态单定义")
    @GetMapping("/del")
    @PreAuthorize("@ss.hasPerm('form:definition:delete')")
    public Result<Void> deleteFormDefinitions(@Parameter(description = "动态单定义ID，多个以英文逗号(,)分割") @RequestParam String ids) {
        boolean result = formDefinitionService.deleteFormDefinitions(ids);
        return Result.judge(result);
    }


    @Operation(summary = "回滚表单历史版本")
    @GetMapping("/history/rollback")
    @PreAuthorize("@ss.hasPerm('form:definition:edit')")
    public Result<Void> rollbackFormDefinitionHistory(@Parameter(description = "动态表单") @RequestParam String formCode,@RequestParam Long version) {
        boolean result = formDefinitionService.rollbackFormDefinitionHistory(formCode,version, 1L);
        return Result.judge(result);
    }


}
